package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import org.springframework.util.CollectionUtils;
import com.atguigu.gmall.product.model.BaseCategoryView;
import com.atguigu.gmall.product.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * VIEW 业务实现类
 *
 * @author atguigu
 * @since 2023-04-21
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    /**
     * 查询所有分类列表 分类嵌套结果:一级分类分类对象中包含二级分类集合;在二级分类对象中包含三级分类集合
     *
     * @return
     */
    @Override
    @GmallCache(prefix = "categoryList:")
    public List<JSONObject> getBaseCategoryList() {
        //1.查询分类视图获取所有的分类集合包含所有三级分类数据
        List<BaseCategoryView> allCategoryViewList = this.list();
        if (!CollectionUtils.isEmpty(allCategoryViewList)) {
            //2.遍历所有视图集合,
            //根据BaseCategoryView里getCategory1Id进行分组
            Map<Long, List<BaseCategoryView>> map1 = allCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
            ArrayList<JSONObject> allJSONObjectList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(map1)) {
                //创建所有一级分类的集合
                int index = 0;
                for (Map.Entry<Long, List<BaseCategoryView>> category1Entry : map1.entrySet()) {
                    Long categoryId = category1Entry.getKey();
                    //获取当前一级分类集合中第一条记录名称
                    String categoryName = category1Entry.getValue().get(0).getCategory1Name();
                    //创建一级分类json对象
                    JSONObject category1 = new JSONObject();
                    category1.put("index", index++);
                    category1.put("categoryName", categoryName);
                    category1.put("categoryId", categoryId);
                    //对当前一级分类集合再次进行按照二级分类ID分组Map key:二级分类ID Val:所有二级分类集合
                    Map<Long, List<BaseCategoryView>> map2 = category1Entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
                    if (!CollectionUtils.isEmpty(map2)) {
                        ArrayList<JSONObject> category2JsonObjectList = new ArrayList<>();
                        for (Map.Entry<Long, List<BaseCategoryView>> category2Entry : map2.entrySet()) {
                            Long category2Id = category2Entry.getKey();
                            String category2Name = category2Entry.getValue().get(0).getCategory2Name();
                            //创建2级分类json对象
                            JSONObject category2 = new JSONObject();
                            category2.put("categoryId", category2Id);
                            category2.put("categoryName", category2Name);
                            category2JsonObjectList.add(category2);
                            Map<Long, List<BaseCategoryView>> map3 = category2Entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
                            if (!CollectionUtils.isEmpty(map3)) {
                                ArrayList<JSONObject> category3JsonObjectList = new ArrayList<>();
                                for (Map.Entry<Long, List<BaseCategoryView>> category3Entry : map3.entrySet()) {
                                    Long category3Id = category3Entry.getKey();
                                    String category3Name = category3Entry.getValue().get(0).getCategory3Name();
                                    //创建3级分类json对象
                                    JSONObject category3 = new JSONObject();
                                    category3.put("categoryId", category3Id);
                                    category3.put("categoryName", category3Name);
                                    category3JsonObjectList.add(category3);
                                }
                                category2.put("categoryChild", category3JsonObjectList);
                            }
                        }
                        category1.put("categoryChild", category2JsonObjectList);

                    }
                    allJSONObjectList.add(category1);
                }
            }
            return allJSONObjectList;
        }


        return null;
    }
}
